package com.sb.service.impl;

import com.sb.dao.AreaDao;
import com.sb.entity.Area;
import com.sb.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author 小柯
 * @Date 2019/11/24  21:41
 **/
@Service
public class AreaServiceImpl implements AreaService {

    @Autowired
    private AreaDao areaDao;

    @Override
    public List<AreaDao> queryArea() {
        return areaDao.queryArea();
    }

    @Override
    public Area queryAreaById(int areaId) {
        int i = 1/0;
        return areaDao.queryAreaById(areaId);
    }

    @Transactional
    @Override
    public boolean insertArea(Area area) {

        if(area != null && !"".equals(area.getAreaName())){
            area.setCreateTime(new Date());
            area.setLastEditTime(new Date());
            try{
                int i = areaDao.insertArea(area);
                if(i > 0){
                    return true;
                }else {
                    throw  new RuntimeException("插入区域信息错误");
                }
            }catch (Exception e){
                throw  new RuntimeException("插入区域信息错误" + e.getMessage());
            }

        }
        return false;
    }


    @Transactional
    @Override
    public boolean updateArea(Area area) {
        if(area.getAreaId() != null & area.getAreaId()>0){
            area.setLastEditTime(new Date());
            try{
                int i = areaDao.updateArea(area);
                if(i > 0){
                    return true;
                }else {
                    throw  new RuntimeException("更新区域信息错误");
                }
            }catch (Exception e){
                throw  new RuntimeException("更新区域信息错误" + e.getMessage());
            }
        }
        return false;
    }

    @Override
    @Transactional
    public boolean deleteArea(int areaId) {


        if(areaId>0){
            try{
                int i = areaDao.deleteArea(areaId);
                if(i > 0){
                    return true;
                }else {
                    throw  new RuntimeException("删除区域信息错误");
                }
            }catch (Exception e){
                throw  new RuntimeException("删除区域信息错误" + e.getMessage());
            }
        }
        return false;
    }
}
